<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 用户等级
 * Class UserLevelController
 * @package App\Http\Controllers\Admin
 */
class UserLevelController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        //查询用户游戏数据
        $list = DB::table('user_level')
            ->orderBy('need_money', 'asc')
            ->get();
        return view('admin.userLevel.index', ['list' => $list]);
    }

    public function edit($id)
    {
        $detail = DB::table('user_level')->find($id);
        return view('admin.userLevel.edit', ['detail' => $detail]);
    }

    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'title' => ['required','string', 'max:16'],
            'icon' => [],
            'need_money' => ['required','int'],
            'min_money' => ['required','int'],
            'ly_rate' => ['required','int'],
            'integral_rate' => ['required','int'],
            'jr_gift' => [],
            'zskf_gift' => [],
            'day_reward' => [],
        ]);
        if (!$request->icon) {
            $request->icon = '';
        }
        if($id != 0){
            // 更新
            DB::table('user_level')
                ->where('id', $id)
                ->update([
                    'title' => $request->title,
                    'icon' => $request->icon,
                    'need_money' => $request->need_money,
                    'ly_rate' => $request->ly_rate,
                    'integral_rate' => $request->integral_rate,
                    'min_money' => $request->min_money,
                    'jr_gift' => $request->jr_gift,
                    'zskf_gift' => $request->zskf_gift,
                    'day_reward' => $request->day_reward,
                ]);
        }else{
            // 创建
            DB::table('user_level')
                ->insert([
                    'title' => $request->title,
                    'icon' => $request->icon,
                    'need_money' => $request->need_money,
                    'ly_rate' => $request->ly_rate,
                    'integral_rate' => $request->integral_rate,
                    'min_money' => $request->min_money,
                    'jr_gift' => $request->jr_gift,
                    'zskf_gift' => $request->zskf_gift,
                    'day_reward' => $request->day_reward,
                ]);
        }
        return successResp('');
    }

    public function destroy($id)
    {
        DB::table('user_level')
            ->whereIn('id', explode(',', $id))
            ->delete();
        return successResp('');
    }

    public function getList()
    {
        //查询用户游戏数据
        $list = DB::table('user_level')
            ->orderBy('need_money', 'asc')
            ->get();
        return successResp($list);
    }
}
